/*
* Copyright 2016 YONYOU Corporation Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project         marketing

* @Package         com.yonyou.marketing.message.spi.webchattools

* @Author          LuZhen

* @FileName        WxMpTemplateMsgServiceImpl

* @Date            2016/12/9

----------------------------------------------------------------------------------
*     Date          Who             Version         Comments
* 1. 2016/12/9        LuZhen          1.0             Create Source
*
----------------------------------------------------------------------------------
*/
package com.yonyou.gmmc.service.wechatpush.spi.webchattools;

import com.alibaba.fastjson.JSON;
import com.yonyou.gmmc.service.wechatpush.common.HttpClientUtil;

import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;

@Service
public class WxMpTemplateMsgServiceImpl implements WxMpTemplateMsgService {

    private final static Logger logger = Logger.getLogger(WxMpTemplateMsgServiceImpl.class);

//    @Autowired
//    private RestTemplate restTemplate;

    @Autowired
    private WebChatToolsConfig config;


    //private static final String URL="{\"template_id_short\":\"%1$s\"}";

    @Override
    public WxMpSendTemplateMessageResponse sendTemplateMsg(WxMpTemplateMessage templateMessage, String accessToken) {

        WxMpSendTemplateMessageResponse responseContent;
        try {
//            responseContent = restTemplate.postForEntity
//                    (config.getWebChatTemplateSendUrl()+"?access_token="+accessToken,
//                    templateMessage,WxMpSendTemplateMessageResponse.class).getBody();
            logger.debug("postdata is "+templateMessage.toJson());
            String body=HttpClientUtil.sendPostRequest(config.getWebChatTemplateSendUrl()+"?access_token="+accessToken, templateMessage.toJson());
            responseContent = JSON.parseObject(body,WxMpSendTemplateMessageResponse.class);
        } catch (RestClientException e) {
            logger.error(e);
            responseContent = new WxMpSendTemplateMessageResponse();
            responseContent.setErrorCode(-1);
            responseContent.setErrorMessage("fail");
            responseContent.setMessageId("-1");
            return responseContent;
        }
        return responseContent;
    }

    @Override
    public WxMpAddTemplateResponse addTemplate(String shortTemplateId,String accessToken) {
        WxMpAddTemplateResponse responseContent ;
        WxMpAddTemplateRequest request = new WxMpAddTemplateRequest();
        request.setTemplateShortId(shortTemplateId);
        try {
//            responseContent = restTemplate.postForEntity
//                    (config.getWebChatTemplateAddUrl()+"?access_token="+accessToken, request,WxMpAddTemplateResponse.class).getBody();

            String body=HttpClientUtil.sendPostRequest(config.getWebChatTemplateAddUrl()+"?access_token="+accessToken, JSON.toJSONString(request));
            responseContent = JSON.parseObject(body,WxMpAddTemplateResponse.class);
        } catch (RestClientException e) {
            logger.error(e);
            responseContent = new WxMpAddTemplateResponse();
            responseContent.setErrorCode(-1);
            responseContent.setErrorMessage("fail");
            responseContent.setTemplateId("-1");
            return  responseContent;
        }
        return responseContent;
    }
}
